package com.lingchou.cloud.process.client.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.TimeZone;

public class LocalDateAndLocalDateTimeUtil {
    private LocalDateAndLocalDateTimeUtil() {
        throw new IllegalStateException("Utility class");
    }

    /**
     * Date 转 LocalDateTime
     *
     * @param date Date 数据
     * @return LocalDateTime
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        return date == null ? null :
                LocalDateTime.ofInstant(date.toInstant(), TimeZone.getDefault().toZoneId());
    }

    /**
     * Date 转 LocalDate
     *
     * @param date Date 数据
     * @return LocalDate
     */
    public static LocalDate dateToLocalDate(Date date) {
        return date == null ? null :
                LocalDateTime.ofInstant(date.toInstant(), TimeZone.getDefault().toZoneId()).toLocalDate();
    }

    /**
     * LocalDate 转 Date
     *
     * @param localDate localDate
     * @return Date
     */
    public static Date localDateToDate(LocalDate localDate) {
        return localDate == null ? null : Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /**
     * LocalDate 转 Date
     *
     * @param localDateTime localDateTime
     * @return Date
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return localDateTime == null ? null : Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }
}
